import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import '../flutterdialog/MyDialog.dart';

class DialogPage extends StatefulWidget {
  DialogPage({Key key}) : super(key: key);

  @override
  _DialogPageState createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  //两种方式 1.在方法中直接处理 2.用 asyns 和await 输出值在处理
  _alertDialog() async {
    var result = await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text("提示信息"),
            content: Text("确定要删除吗？"),
            actions: [
              FlatButton(
                child: Text("取消"),
                onPressed: () {
                  print("取消");
                  Navigator.pop(context, "cancle");
                },
              ),
              FlatButton(
                child: Text("确定"),
                onPressed: () {
                  Navigator.pop(context, "ok");
                },
              )
            ],
          );
        });
    print(result);
  }

  _simpleDialog() {
    showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Text("选择内容"),
            children: [
              SimpleDialogOption(
                child: Text("Option A"),
                onPressed: () {
                  print("A");
                },
              ),
              Divider(),
              SimpleDialogOption(
                child: Text("Option B"),
                onPressed: () {
                  print("B");
                },
              ),
              Divider(),
              SimpleDialogOption(
                child: Text("Option C"),
                onPressed: () {
                  print("C");
                },
              )
            ],
          );
        });
  }

  _modalBottomSheet() async {
    var result = await showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 200,
            child: Column(
              children: [
                ListTile(
                  title: Text("分享A"),
                  onTap: () {
                    Navigator.pop(context, "分享A");
                  },
                ),
                Divider(),
                ListTile(
                  title: Text("分享B"),
                  onTap: () {
                    Navigator.pop(context, "分享B");
                  },
                ),
                Divider(),
                ListTile(
                  title: Text("分享C"),
                  onTap: () {
                    Navigator.pop(context, "分享C");
                  },
                )
              ],
            ),
          );
        });
    print(result);
  }

  //第三方插件 android  Toast
  _toast() {
    Fluttertoast.showToast(
        msg: "提示信息",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dialog弹窗"),
      ),
      body: Column(
        children: [
          RaisedButton(
            child: Text("alertDialog"),
            onPressed: this._alertDialog,
          ),
          RaisedButton(
            child: Text("simpleDialog"),
            onPressed: this._simpleDialog,
          ),
          RaisedButton(
            child: Text("modelBottomSheet"),
            onPressed: this._modalBottomSheet,
          ),
          RaisedButton(
            child: Text("Toast"),
            onPressed: this._toast,
          ),
          RaisedButton(
            child: Text("自定义Dialog"),
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (context) {
                    return MyDialog(
                      title: "自定义Dialog",
                      content: "这是一个自定义Dialog",
                    );
                  });
            },
          )
        ],
      ),
    );
  }
}
